<?php
/**
 * @author : xjw129xjt 531201650@qq.com
 */

namespace app\user\model;

use think\facade\Cache;
use think\Model;

class User extends Model
{
    protected $rule = [];
    public $m_error = '';

    public function userInfo()
    {
        return $this->hasOne('UserInfo', 'uid', 'id');
    }

    public function register($username = null, $email = null, $mobile = null, $nickname = null, $password = '', $ext = [])
    {
        if (empty($username) && empty($email) && empty($mobile)) {
            return false;
        }
        $this->username = $username;
        $this->email = $email;
        $this->mobile = $mobile;
        $this->status = !empty($ext['status']) ? $ext['status'] : 1;
        $this->password = $password;
        $this->create_time = !empty($ext['create_time']) ? $ext['create_time'] : time();
        $this->encrypt_type = !empty($ext['encrypt_type']) ? $ext['encrypt_type'] : 'encrypt';
        $this->nickname = $nickname;
        $this->together(['userInfo' => ['nickname']])->save();

        return $this->id;
    }


    public function setPasswordAttr($value, $data = [])
    {
        return md5(AUTH_KEY . $value); //todo 扩展多种加密方式
    }


    public function getUserInfo($uid)
    {
        static $info1 = []; //使用静态变量，避免多次取缓存
        if (isset($info1[$uid])) {
            return $info1[$uid];
        }
        $info = Cache::remember("user_{$uid}", function () use ($uid) {
            $info = $this->get($uid);
            $userinfo = $info->userInfo->toArray();
            $info = $info->toArray();
            unset($info['userInfo']);

            list($p0, $p1) = model('core/Picture')->renderImgPath($userinfo['avatar']);
            $userinfo['avatar'] = $p1;
            $userinfo['avatar_64'] = model('core/Picture')->getThumbPath($p1, 64, 64);
            $userinfo['avatar_128'] = model('core/Picture')->getThumbPath($p1, 128, 128);
            $userinfo['avatar_256'] = model('core/Picture')->getThumbPath($p1, 256, 256);


            return array_merge($info, $userinfo);
        });
        /*  todo 5.1助手函数暂不支持
                $info = cache("?user_{$uid}", function () use ($uid) {
                    $info = $this->get($uid);
                    $userinfo = $info->userInfo->toArray();
                    $info = $info->toArray();
                    unset($info['userInfo']);
                    return array_merge($info, $userinfo);
                });
                */


        $info1[$uid] = $info;
        return $info;
    }


    public function login($account, $password, $ext = [])
    {
        if (check_is_email($account)) {
            $type = 'email';
        } elseif (check_is_mobile($account)) {
            $type = 'mobile';
        } else {
            $type = 'username';
        }
        $user = $this->where($type, $account)->find();
        if (empty($user) || $user['status'] != 1) {
            $this->m_error = '用户不存在~';
            return false;
        }
        if ($this->setPasswordAttr($password) != $user['password']) {
            $this->m_error = '密码错误~';
            return false;
        }
        return $user['id'];
    }


    public function doLogin($uid)
    {
        $user = $this->getUserInfo($uid);
        $auth = [
            'uid' => $user['uid'],
            'login_time' => time(),
        ];
        session('user_auth', $auth);
        session('user_auth_sign', md5(implode('|', $auth)));
    }

    public function doLogout()
    {
        session('user_auth', null);
        session('user_auth_sign', null);
    }


    public function updateUserInfo($uid, $info = [])
    {
        $data = $idata = [];
        !empty($info['username']) && $data['username'] = $info['username'];
        !empty($info['password']) && $data['password'] = $info['password'];
        isset($info['email']) && $data['email'] = $info['email'];
        isset($info['mobile']) && $data['mobile'] = $info['mobile'];
        !empty($info['nickname']) && $idata['nickname'] = $info['nickname'];
        !empty($info['birthday']) && $idata['birthday'] = $info['birthday'];
        isset($info['sex']) && $idata['sex'] = $info['sex'];
        isset($info['signature']) && $idata['signature'] = $info['signature'];
        !empty($info['avatar']) && $idata['avatar'] = $info['avatar'];
        $rs1 = $rs2 = 1;
        if ($idata) {
            $rs1 = db('user_info')->where('uid', $uid)->update($idata);
        }
        if ($data) {
            $rs2 = db('user')->where('id', $uid)->update($data);
        }
        return $rs1 && $rs2;

    }




}

